package org.jivesoftware.smackx;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.Cache;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverItems;
import org.jivesoftware.smackx.packet.DiscoverItems.Item;
import org.jivesoftware.smackx.packet.MultipleAddresses;
import org.jivesoftware.smackx.packet.MultipleAddresses.Address;

public class MultipleRecipientManager
{
  private static Cache services = new Cache(100, 86400000L);

  private static String getMultipleRecipienServiceAddress(Connection paramConnection)
  {
    Cache localCache1;
    String str1 = paramConnection.getServiceName();
    String str2 = (String)services.get(str1);
    if (str2 == null)
    {
      localCache1 = services;
      monitorenter;
    }
    try
    {
      if (str2 == null);
    }
    finally
    {
      while (true)
      {
        try
        {
          if (ServiceDiscoveryManager.getInstanceFor(paramConnection).discoverInfo(str1).containsFeature("http://jabber.org/protocol/address"))
            str2 = str1;
          while (true)
          {
            boolean bool;
            Cache localCache2 = services;
            if (str2 != null)
              break;
            str4 = "";
            localCache2.put(str1, str4);
            monitorexit;
            if (!("".equals(str2)))
              break label192;
            str3 = null;
            return str3;
            Iterator localIterator = ServiceDiscoveryManager.getInstanceFor(paramConnection).discoverItems(str1).getItems();
            do
            {
              if (!(localIterator.hasNext()));
              DiscoverItems.Item localItem = (DiscoverItems.Item)localIterator.next();
              bool = ServiceDiscoveryManager.getInstanceFor(paramConnection).discoverInfo(localItem.getEntityID(), localItem.getNode()).containsFeature("http://jabber.org/protocol/address");
            }
            while (!(bool));
            str2 = str1;
          }
          String str4 = str2;
        }
        catch (XMPPException localXMPPException)
        {
          while (true)
            localXMPPException.printStackTrace();
          localObject = finally;
          monitorexit;
          throw localObject;
        }
        label192: String str3 = str2;
      }
    }
  }

  public static MultipleRecipientInfo getMultipleRecipientInfo(Packet paramPacket)
  {
    MultipleRecipientInfo localMultipleRecipientInfo;
    MultipleAddresses localMultipleAddresses = (MultipleAddresses)paramPacket.getExtension("addresses", "http://jabber.org/protocol/address");
    if (localMultipleAddresses == null)
      localMultipleRecipientInfo = null;
    while (true)
    {
      return localMultipleRecipientInfo;
      localMultipleRecipientInfo = new MultipleRecipientInfo(localMultipleAddresses);
    }
  }

  public static void reply(Connection paramConnection, Message paramMessage1, Message paramMessage2)
    throws XMPPException
  {
    MultipleRecipientInfo localMultipleRecipientInfo = getMultipleRecipientInfo(paramMessage1);
    if (localMultipleRecipientInfo == null)
      throw new XMPPException("Original message does not contain multiple recipient info");
    if (localMultipleRecipientInfo.shouldNotReply())
      throw new XMPPException("Original message should not be replied");
    if (localMultipleRecipientInfo.getReplyRoom() != null)
      throw new XMPPException("Reply should be sent through a room");
    if (paramMessage1.getThread() != null)
      paramMessage2.setThread(paramMessage1.getThread());
    MultipleAddresses.Address localAddress = localMultipleRecipientInfo.getReplyAddress();
    if ((localAddress != null) && (localAddress.getJid() != null))
    {
      paramMessage2.setTo(localAddress.getJid());
      paramConnection.sendPacket(paramMessage2);
    }
    while (true)
    {
      Iterator localIterator2;
      return;
      ArrayList localArrayList1 = new ArrayList();
      ArrayList localArrayList2 = new ArrayList();
      Iterator localIterator1 = localMultipleRecipientInfo.getTOAddresses().iterator();
      if (!(localIterator1.hasNext()))
        localIterator2 = localMultipleRecipientInfo.getCCAddresses().iterator();
      while (true)
      {
        while (true)
        {
          while (true)
          {
            if (localIterator2.hasNext())
              break label312;
            if ((!(localArrayList1.contains(paramMessage1.getFrom()))) && (!(localArrayList2.contains(paramMessage1.getFrom()))))
              localArrayList1.add(paramMessage1.getFrom());
            String str1 = paramConnection.getUser();
            if ((!(localArrayList1.remove(str1))) && (!(localArrayList2.remove(str1))))
            {
              String str3 = StringUtils.parseBareAddress(str1);
              localArrayList1.remove(str3);
              localArrayList2.remove(str3);
            }
            String str2 = getMultipleRecipienServiceAddress(paramConnection);
            if (str2 == null)
              break label336;
            sendThroughService(paramConnection, paramMessage2, localArrayList1, localArrayList2, null, null, null, false, str2);
          }
          localArrayList1.add(((MultipleAddresses.Address)localIterator1.next()).getJid());
        }
        label312: localArrayList2.add(((MultipleAddresses.Address)localIterator2.next()).getJid());
      }
      label336: sendToIndividualRecipients(paramConnection, paramMessage2, localArrayList1, localArrayList2, null);
    }
  }

  public static void send(Connection paramConnection, Packet paramPacket, List paramList1, List paramList2, List paramList3)
    throws XMPPException
  {
    send(paramConnection, paramPacket, paramList1, paramList2, paramList3, null, null, false);
  }

  public static void send(Connection paramConnection, Packet paramPacket, List paramList1, List paramList2, List paramList3, String paramString1, String paramString2, boolean paramBoolean)
    throws XMPPException
  {
    String str = getMultipleRecipienServiceAddress(paramConnection);
    if (str != null)
      sendThroughService(paramConnection, paramPacket, paramList1, paramList2, paramList3, paramString1, paramString2, paramBoolean, str);
    while (true)
    {
      return;
      if ((paramBoolean) || ((paramString1 != null) && (paramString1.trim().length() > 0)) || ((paramString2 != null) && (paramString2.trim().length() > 0)))
        throw new XMPPException("Extended Stanza Addressing not supported by server");
      sendToIndividualRecipients(paramConnection, paramPacket, paramList1, paramList2, paramList3);
    }
  }

  private static void sendThroughService(Connection paramConnection, Packet paramPacket, List paramList1, List paramList2, List paramList3, String paramString1, String paramString2, boolean paramBoolean, String paramString3)
  {
    Iterator localIterator1;
    Iterator localIterator2;
    Iterator localIterator3;
    MultipleAddresses localMultipleAddresses = new MultipleAddresses();
    if (paramList1 != null)
    {
      localIterator3 = paramList1.iterator();
      if (localIterator3.hasNext())
        break label105;
    }
    if (paramList2 != null)
    {
      localIterator2 = paramList2.iterator();
      if (localIterator2.hasNext())
        break label129;
    }
    if (paramList3 != null)
    {
      localIterator1 = paramList3.iterator();
      if (localIterator1.hasNext())
        break label153;
    }
    if (paramBoolean)
      localMultipleAddresses.setNoReply();
    while (true)
    {
      do
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              paramPacket.setTo(paramString3);
              paramPacket.addExtension(localMultipleAddresses);
              paramConnection.sendPacket(paramPacket);
              return;
              label105: localMultipleAddresses.addAddress("to", (String)localIterator3.next(), null, null, false, null);
            }
            label129: localMultipleAddresses.addAddress("cc", (String)localIterator2.next(), null, null, false, null);
          }
          label153: localMultipleAddresses.addAddress("bcc", (String)localIterator1.next(), null, null, false, null);
        }
        if ((paramString1 != null) && (paramString1.trim().length() > 0))
          localMultipleAddresses.addAddress("replyto", paramString1, null, null, false, null);
      }
      while ((paramString2 == null) || (paramString2.trim().length() <= 0));
      localMultipleAddresses.addAddress("replyroom", paramString2, null, null, false, null);
    }
  }

  private static void sendToIndividualRecipients(Connection paramConnection, Packet paramPacket, List paramList1, List paramList2, List paramList3)
  {
    Iterator localIterator1;
    Iterator localIterator2;
    Iterator localIterator3;
    if (paramList1 != null)
    {
      localIterator3 = paramList1.iterator();
      if (localIterator3.hasNext())
        break label69;
    }
    if (paramList2 != null)
    {
      localIterator2 = paramList2.iterator();
      if (localIterator2.hasNext())
        break label101;
    }
    if (paramList3 != null)
      localIterator1 = paramList3.iterator();
    while (true)
    {
      while (true)
      {
        while (true)
        {
          if (localIterator1.hasNext())
            break label133;
          return;
          label69: paramPacket.setTo((String)localIterator3.next());
          paramConnection.sendPacket(new PacketCopy(paramPacket.toXML()));
        }
        label101: paramPacket.setTo((String)localIterator2.next());
        paramConnection.sendPacket(new PacketCopy(paramPacket.toXML()));
      }
      label133: paramPacket.setTo((String)localIterator1.next());
      paramConnection.sendPacket(new PacketCopy(paramPacket.toXML()));
    }
  }

  private static class PacketCopy extends Packet
  {
    private String text;

    public PacketCopy(String paramString)
    {
      this.text = paramString;
    }

    public String toXML()
    {
      return this.text;
    }
  }
}